package com.xhs.zdl.demo.ListViewFragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.xhs.zdl.demo.R;
import com.xhs.zdl.demo.adapter.NormalListViewAdapter;

import java.util.ArrayList;

public class ListViewActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> data;
    private NormalListViewAdapter adapter;
    private ListViewShowFragment tempFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        initView();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listView_fragment_listView);

        data = new ArrayList<>();
        adapter = new NormalListViewAdapter(this, data);
        listView.setAdapter(adapter);

        data.add("条目1");
        data.add("条目2");
        data.add("条目3");
        data.add("条目4");
        data.add("条目5");
        data.add("条目6");
        data.add("条目7");
        data.add("条目8");
        data.add("条目9");
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(onItemClickListener);
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            showFragment(i);
        }
    };

    private void showFragment(int i){
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        if (null != tempFragment)
            transaction.hide(tempFragment);
        ListViewShowFragment fragment = new ListViewShowFragment();
        tempFragment = fragment;
        Bundle bundle = new Bundle();
        bundle.putString("data", data.get(i));
        fragment.setArguments(bundle);
        transaction.add(R.id.listView_fragment_ll, fragment);

        transaction.commit();
    }
}
